package com.sxhuayuan.parking.controller.admin;

import javax.annotation.Resource;

import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.service.AdminService;
import org.springframework.web.bind.annotation.*;

import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.entity.OemConfig;
import com.sxhuayuan.parking.service.OemConfigService;

/**
 * 渠道配置
 * 
 * @author gy
 */
@RestController("adminOemConfigController")
@RequestMapping("/admin/oemconfig")
public class OemConfigController extends BaseController {

	@Resource(name = "oemConfigServiceImpl")
	OemConfigService service;
	@Resource(name = "adminServiceImpl")
	AdminService adminService;

	@RequestMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable) {
		Page<OemConfig> page = service.findPage(pageable);
		return RespMessage.success(page);
	}

	@PostMapping("/save")
	public RespMessage save(@RequestBody OemConfig config) {
		if (!isValid(config)) {
			return getValidErrorInfo();
		}
		service.save(config);
		return RespMessage.success();
	}

	@PostMapping("/update")
	public RespMessage update(@RequestBody OemConfig config) {
		if (!isValid(config)) {
			return getValidErrorInfo();
		}
		if (config.getId() == null || service.findOne(config.getId()) == null) {
			return RespMessage.error("配置不存在");
		}
		service.update(config);
		return RespMessage.success();
	}

	@PostMapping("/del/{id}")
	public RespMessage del(@PathVariable Long id) {
		OemConfig one = service.findOne(id);
		if (one == null) {
			return RespMessage.error("配置不存在");
		}
		service.delete(id);
		return RespMessage.success();
	}

	@GetMapping("/get/all")
	public RespMessage getAll() {
		if (!adminService.isAuthenticated()) {
			return invalidToken();
		}
		return successResp(service.findAll());
	}

	@GetMapping("/{id}")
	public RespMessage getOne(@PathVariable Long id) {
		return successResp(service.findOne(id));
	}
}
